﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Practices.Unity;
using System.Windows.Input;
using MP3Ganizer.Common.Command;
using MP3Ganizer.Events.User;

namespace MP3Ganizer.Modules.DirectoryLoader.ViewModels
{
  /// <summary>
  /// ViewModel du DirectoryLoader
  /// </summary>
  public class DirectoryLoaderMainVM: IDirectoryLoaderMainVM, INotifyPropertyChanged
  {
    #region Fields

    private readonly IUnityContainer _container;

    private String _sourceDirectory;

    /// <summary>
    /// Répertoire source
    /// </summary>
    public String SourceDirectory
    {
      get { return _sourceDirectory; }
      set 
      { 
        _sourceDirectory = value;
        OnPropertyChanged("SourceDirectory");
      }
    }

    private String _destinationDirectory;

    /// <summary>
    /// Répertoire destination
    /// </summary>
    public String DestinationDirectory
    {
        get { return _destinationDirectory; }
        set
        {
            _destinationDirectory = value;
            OnPropertyChanged("DestinationDirectory");
        }
    }
    #endregion
    
    #region IDirectoryLoaderMainVM Members

    /// <summary>
    /// Vue principale
    /// </summary>
    public MP3Ganizer.Modules.DirectoryLoader.Views.IDirectoryLoaderMainV MainView {get; private set;}

    #endregion

    /// <summary>
    /// Constructeur. Les paramètres sont pré-remplis par le UnityContainer.
    /// </summary>
    /// <param name="container"></param>
    /// <param name="mainView"></param>
    public DirectoryLoaderMainVM(IUnityContainer container, MP3Ganizer.Modules.DirectoryLoader.Views.IDirectoryLoaderMainV mainView)
    {
      this._container = container;
      this.MainView = mainView;
      this.MainView.SetModel(this);
      this._container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<MP3Ganizer.Events.User.UserActionEvent>().Subscribe(OnUserAction, Microsoft.Practices.Composite.Presentation.Events.ThreadOption.UIThread, true);
    }

    private void OnUserAction(MP3Ganizer.Events.User.UserActionEventArgs e)
    {
       
    }

    #region INotifyPropertyChanged Members
    /// <summary>
    /// Implémentation de INotifyPropertyChanged.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Fonction de Raise de l'évènement PropertyChanged.
    /// </summary>
    /// <param name="PropertyName"></param>
    protected void OnPropertyChanged(string PropertyName)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
      }
    }

    #endregion

    #region Commands
    #region LoadData
    private ICommand _loadDataCommand;
    /// <summary>
    /// Commande permettant de charger un module à la demande
    /// </summary>
    public ICommand LoadDataCommand
    {
        get
        {
            if (this._loadDataCommand == null)
                this._loadDataCommand = new RelayCommand<object>((item) => this.LoadData(), (item) => this.CanLoadData());

            return this._loadDataCommand;
        }
    }
    /// <summary>
    /// Fonction chargeant un module à la demande
    /// </summary>
    /// <param name="parameter"></param>
    public void LoadData()
    {
        try
        {
            UserActionEventArgs e = new UserActionEventArgs(Common.Enums.UserAction.LoadData, new KeyValuePair<string, string>(_sourceDirectory, _destinationDirectory));
            _container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<UserActionEvent>().Publish(e);
        }
        catch (Exception ex)
        {

        }
    }
    private bool CanLoadData()
    {        
        return !string.IsNullOrEmpty(_destinationDirectory) && !string.IsNullOrEmpty(_sourceDirectory);
    }
    #endregion

    #region SaveData
    private ICommand _saveDataCommand;
    /// <summary>
    /// Commande permettant de charger un module à la demande
    /// </summary>
    public ICommand SaveDataCommand
    {
        get
        {
            if (this._saveDataCommand == null)
                this._saveDataCommand = new RelayCommand<object>((item) => this.SaveData(), (item) => this.CanSaveData());

            return this._saveDataCommand;
        }
    }
    /// <summary>
    /// Fonction chargeant un module à la demande
    /// </summary>
    /// <param name="parameter"></param>
    public void SaveData()
    {
        try
        {

            Events.User.ShowMediaData data = new Events.User.ShowMediaData("", "", "", "", Common.Enums.DataType.Unknown);
            this._container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<MP3Ganizer.Events.User.UserActionEvent>().Publish(new Events.User.UserActionEventArgs(Common.Enums.UserAction.ShowMedia, data, SaveDataCallBack));
            
        }
        catch (Exception ex)
        {

        }
    }

    public void SaveDataCallBack()
    {
        UserActionEventArgs e = new UserActionEventArgs(Common.Enums.UserAction.SaveData);
        _container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<UserActionEvent>().Publish(e);
    }
    private bool CanSaveData()
    {
        return !string.IsNullOrEmpty(_destinationDirectory) && !string.IsNullOrEmpty(_sourceDirectory);
    }
    #endregion

    #region CopyToDestination
    private ICommand _copyToDestinationCommand;
    /// <summary>
    /// Commande permettant de charger un module à la demande
    /// </summary>
    public ICommand CopyToDestinationCommand
    {
        get
        {
            if (this._copyToDestinationCommand == null)
                this._copyToDestinationCommand = new RelayCommand<object>((item) => this.CopyToDestination(), (item) => this.CanCopyToDestination());

            return this._copyToDestinationCommand;
        }
    }
    /// <summary>
    /// Fonction chargeant un module à la demande
    /// </summary>
    /// <param name="parameter"></param>
    public void CopyToDestination()
    {
        try
        {
            Events.User.ShowMediaData data = new Events.User.ShowMediaData("", "", "", "", Common.Enums.DataType.Unknown);
            this._container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<MP3Ganizer.Events.User.UserActionEvent>().Publish(new Events.User.UserActionEventArgs(Common.Enums.UserAction.ShowMedia, data, CopyToDestinationCallBack));
        }
        catch (Exception ex)
        {

        }
    }
    public void CopyToDestinationCallBack()
    {
        UserActionEventArgs e = new UserActionEventArgs(Common.Enums.UserAction.CopyToDestination, LoadData);
        _container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<UserActionEvent>().Publish(e);
    }
    private bool CanCopyToDestination()
    {
        return !string.IsNullOrEmpty(_destinationDirectory) && !string.IsNullOrEmpty(_sourceDirectory);
    }
    #endregion
    #endregion
  }
}
